package com.p2p.front.util;

import com.p2p.base.model.Money;
import com.p2p.front.enums.LoanRepayMethod;

public class RateUtil {

	public static void main(String[] args) {
		/* monthApr = apr/12/100; // 月利率
         当repay_method=m(等额本息)时，
         interest = money * monthApr * pow((1+monthApr), deadline) / (i-1) * deadline - money;

         当repay_method=i(按月付息，到期还本)或e(到期还本息)时,
         interest = money * monthApr * deadline;*/
		
		
		
		Money b = RateUtil.getMonthRepayAmountForPrincipalAndInterest(0.1,12,new Money(10000,00));
		System.out.println(b);
	}
	
	
	public static Money getAllInterestForNormal(double yearPercentRate,int months,Money principal){
		
		Money monthInterest = getMonthInterestForNormal(yearPercentRate,months,principal);
		return new Money(monthInterest.getCent()*months);
	}
	//按月付息
	public static Money getMonthInterestForNormal(double yearPercentRate,int months,Money principal){
		double yearRate = yearPercentRate/100;
		double monthRate = yearRate/12;
		
		Double a = monthRate*principal.getCent();
		
		return new Money(a.longValue());
	}
	//tender_return的等额本息，存了总量+本金，这个地方只算总量
	//等额本息
	//这个地方的利率应该是：正常利率+平台奖励利率，loan.getReturnApr
	public static Money getMonthRepayAmountForPrincipalAndInterest(double yearPercentRate,int months,Money principal){
		double yearRate = yearPercentRate/100;
		double monthRate = yearRate/12;
		double temp =  Math.pow((monthRate+1),months);
		double coefficient = temp*monthRate/(temp-1);
		
		Double a = coefficient*principal.getCent();
		
		
		return new Money( a.longValue());
	}
	
	public static Money getAllInterest(double yearPercentRate,int months,Money principal, String repayMethod ){
		//double yearRate = yearPercentRate/100;
		LoanRepayMethod method = LoanRepayMethod.getEnumByName(repayMethod);
		if(method==null){
			throw new RuntimeException("LoanRepayMethod not exist,when getAllInterest");
		}
		Money interest = new Money();
		if(method==LoanRepayMethod.e){
			interest = getAllInterestForNormal(yearPercentRate,months,principal);
		}else if(method==LoanRepayMethod.i){
			interest = getAllInterestForNormal(yearPercentRate,months,principal);
		}else if(method==LoanRepayMethod.m){
			//等额本息，月付额度
			Money monthRepay = getMonthRepayAmountForPrincipalAndInterest(yearPercentRate,months,principal);
			
			//总还款金额
			Money allRepay = new Money(monthRepay.getCent()*months);
			
			//总利息
			interest = allRepay.sub(principal);
		}else{
			throw new RuntimeException("repayMethod not support");
		}
		
		return interest;
	}
	
	public static double getDayliRate(double yearPercentRate){
		double yearRate = yearPercentRate/100;
		double dailyRate = yearRate/12/30;
		return dailyRate;
	}
	
}
